package org.sample.net.http;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.Future;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.nio.client.HttpAsyncClient;
import org.apache.http.util.EntityUtils;

public class HttpClientApacheAsyn {

    private static HttpAsyncClient httpclient = initHttpClientBuilder();

    private static final int Timeout = 6000;

    public static void main(String[] args) throws Exception {
        get("http://www.qq.com");
        get("http://www.google.com");
        get("https://www.verisign.com");
        get("http://www.qq.com");
        get("http://www.apache.org");
        get("http://www.baidu.com");

    }

    public static String get(String url) throws Exception {
        return send(url, null, "GET");
    }

    public static String post(String url) throws Exception {
        return post(url, null);
    }

    public static String post(String url, String request) throws Exception {
        return send(url, request, "POST");
    }

    public static String send(String url, String message, String method) throws Exception {

        final HttpUriRequest request;
        HttpResponse response = null;

        if ("POST".equals(method)) {
            StringEntity entity = new StringEntity(message, StandardCharsets.UTF_8);
            entity.setContentType("application/json");
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(entity);
            request = httpPost;
        } else {
            request = new HttpGet(url);
        }

        Future<HttpResponse> execute = httpclient.execute(request, new FutureCallback<HttpResponse>() {
            @Override
            public void completed(final HttpResponse response) {
                System.out.println(request.getRequestLine() + "->" + response.getStatusLine());
            }

            @Override
            public void failed(final Exception ex) {
                System.out.println(request.getRequestLine() + "->" + ex);
            }

            @Override
            public void cancelled() {
                System.out.println(request.getRequestLine() + " cancelled");
            }
        });
        response = execute.get();
        String responseMessage = null;
        if (response != null && response.getEntity() != null) {
            HttpEntity respEntity = response.getEntity();
            responseMessage = EntityUtils.toString(respEntity, StandardCharsets.UTF_8);
        }
        return responseMessage;
    }

    private static HttpAsyncClient initHttpClientBuilder() {
        Builder builder = RequestConfig.custom();
        builder.setSocketTimeout(Timeout);
        builder.setConnectTimeout(Timeout);
        builder.setConnectionRequestTimeout(Timeout);

        RequestConfig requestConfig = builder.build();
        HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create().setDefaultRequestConfig(requestConfig);
        CloseableHttpAsyncClient build = httpClientBuilder.build();
        build.start();
        return build;
    }

}